package com.byxy.ssm.demo.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.byxy.ssm.demo.dao.ForumDao;
import com.byxy.ssm.demo.dao.ReplyDao;
import com.byxy.ssm.demo.dao.TabDao;
import com.byxy.ssm.demo.dao.TipDao;
import com.byxy.ssm.demo.dao.UserDao;
import com.byxy.ssm.demo.entity.Forum;
import com.byxy.ssm.demo.entity.Reply;
import com.byxy.ssm.demo.entity.Tab;
import com.byxy.ssm.demo.entity.Tip;
import com.byxy.ssm.demo.entity.User;
import com.byxy.ssm.demo.service.MainService;
import com.byxy.ssm.demo.vo.TipVO;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service  // 两个注解必须
@Transactional
public class MainServiceImpl implements MainService {
	@Autowired
	private TipDao tipDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private TabDao tabDao;
	@Autowired
	private ForumDao forumDao;
//	@Autowired
//	private ReplyDao replyDao;
	@Override
	public List<TipVO> find() {
		List<Tip> tips = tipDao.find();//与数据库打交道,从数据库取出数据
		List<TipVO> tipvos = new ArrayList<>();
		for(Tip p :tips){
			TipVO vo = new TipVO();
			BeanUtils.copyProperties(p, vo);//将数据取出的数据赋给TipVO集合
			User u = userDao.getById(p.getUserId());
			
			if (u.getUserNick()!= null && u.getUserNick().equals("")) 
				vo.setUserName(u.getUserNick());
			else {
				vo.setUserName(u.getUserName());
				Tab tab = tabDao.getById(p.getTabId());
				vo.setTabName(tab.getTabName());
				Forum forum =forumDao.getById(tab.getForumId());
				vo.setForumName(forum.getForumName());
				log.info("tap="+tab);
				log.info("vo="+vo);
//				Reply reply = replyDao.getById();
			}
			
			
			tipvos.add(vo);
		}
		return tipvos;
	}
	
}
